﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;


namespace TargetControl
{
    public class DrawPolygon
    {
        public int LastX;
        public int LastY;
        private Color color;
        private List<Point> pointArray;         // list of points

        public DrawPolygon(int x1, int y1, int x2, int y2)
        {
            pointArray = new List<Point>();
            pointArray.Add(new Point(x1, y1));
            pointArray.Add(new Point(x2, y2));
            color = Color.SkyBlue;
        }

        public void DrawLine(Graphics g, Point currentPoint)
        {
            Pen pen = new Pen(color);
            g.DrawLine(pen, currentPoint.X, currentPoint.Y, LastX, LastY);
        }
        public void Draw(Graphics g)
        {
            int x1 = 0, y1 = 0;     // previous point
            int x2, y2;             // current point

            g.SmoothingMode = SmoothingMode.AntiAlias;

            Pen pen = new Pen(color);

            IEnumerator<Point> enumerator = pointArray.GetEnumerator();

            if (enumerator.MoveNext())
            {
                x1 = ((Point)enumerator.Current).X;
                y1 = ((Point)enumerator.Current).Y;
            }

            while (enumerator.MoveNext())
            {
                x2 = ((Point)enumerator.Current).X;
                y2 = ((Point)enumerator.Current).Y;

                g.DrawLine(pen, x1, y1, x2, y2);

                x1 = x2;
                y1 = y2;
            }

            pen.Dispose();
        }

        public void AddPoint(Point point)
        {
            pointArray.Add(point);
        }

        public int HandleCount
        {
            get
            {
                return pointArray.Count;
            }
        }

        public void MoveHandleTo(Point point, int handleNumber)
        {
            if (handleNumber < 1)
                handleNumber = 1;

            if (handleNumber > pointArray.Count)
                handleNumber = pointArray.Count;

            pointArray[handleNumber - 1] = point;
        }

    }
}
